package com.tudou.ppjoke;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.tudou.ppjoke.databinding.ActivityMainBinding;
import com.tudou.ppjoke.model.Destination;
import com.tudou.ppjoke.model.User;
import com.tudou.ppjoke.ui.login.UserManager;
import com.tudou.ppjoke.utils.AppConfig;
import com.tudou.ppjoke.utils.NavGraphBuilder;
import com.tudou.ppjoke.utils.StatuseBar;
import com.tudou.ppjoke.view.AppBottomBar;

import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener {

    private ActivityMainBinding binding;
    private NavController navController;
    private AppBottomBar navView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);

        StatuseBar.fitSystemBar(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        BottomNavigationView navView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
//        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
//                R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
//                .build();

        navController= Navigation.findNavController(this, R.id.nav_host_fragment_activity_main);
        Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment_activity_main);
//        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
//        NavigationUI.setupWithNavController(binding.navView, navController);

        NavGraphBuilder.build(navController,this, fragment.getId());

        navView.setOnNavigationItemSelectedListener(this);

        //GetRequest<JSONObject> request=new GetRequest<>("www.baidu,com");
//        request.execute();
//
//        request.execute(new JsonCallback<JSONObject>(){
//            @Override
//            public void onSuccess(ApiResponse<JSONObject> response) {
//                super.onSuccess(response);
//            }
//        });
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull @NotNull MenuItem item) {
        HashMap<String, Destination> destConfig = AppConfig.getsDestConfig();
        Iterator<Map.Entry<String, Destination>> iterator = destConfig.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<String, Destination> entry = iterator.next();
            Destination value = entry.getValue();

            if (value != null && !UserManager.get().isLogin() && value.getNeedLogin() && value.getId() == item.getItemId()){

                UserManager.get().login(this).observe(this, new Observer<User>() {
                    @Override
                    public void onChanged(User user) {
                        if (user != null) {
                            navView.setSelectedItemId(item.getItemId());
                        }
                    }
                });
                return false;
            }
        }


        navController.navigate(item.getItemId());
        return !TextUtils.isEmpty(item.getTitle());
    }
}